/**  
 * @Title: SiteAjaxController.java
 * @Package com.limit.control.site
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-11 上午11:00:03
 * @version V1.0
 */ 
package com.limit.control.site;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.limit.model.Game;
import com.limit.model.GameComment;

/** 
 * <br/>
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-11 上午11:00:03 
 * @version V1.0
 */

public class AjaxSiteController extends BaseSiteController {
	
	/**
	 * more.html 更多游戏页面
	 */
	public void getMoreGames(){
		long rank_id = this.getParaToLong("rid", 0L);
		int p = this.getParaToInt("p", 1);
		List<Game> games = Game.dao.getGamesByRankId(rank_id, p, pageSize).getList();
		System.out.println(games.size());
		if(games.size() < 1){
			renderJson("msg", "none");
			return ;
		}
		String json = JsonKit.listToJson(games, 2);
		this.renderJson(json);
	}
	
	/**
	 * 获取更多评论列表  一次拿8条记录
	 */
	public void getMoreComments(){
		long gid = this.getParaToLong("gid", 0L);
		int p = this.getParaToInt("p", 1);
		List<GameComment> games = GameComment.dao.paginateList(gid, p, pageSize).getList();
		System.out.println(games.size());
		if(games.size() < 1){
			renderJson("msg", "none");
			return ;
		}
		String json = JsonKit.listToJson(games, 2);
		this.renderJson(json);
	}
	
	/**
	 * 点赞
	 */
	public void useful(){
		long cid = this.getParaToLong("cid",0L);
		System.out.println(this.getRequest().getRemoteHost());
		GameComment comment = GameComment.dao.findById(cid);
		long useful = comment.get("useful");
		if(useful < 0) useful = 0;
		boolean isSuccess = comment.set("useful", ++useful).update();;
		if(isSuccess){
			renderJson("result", true);
		}else{
			renderJson("result", false);
		}
	}
	/**
	 * 点弱
	 */
	public void unuse(){
		long cid = this.getParaToLong("cid",0L);
		GameComment comment = GameComment.dao.findById(cid);
		long unuseful = comment.get("unuseful");
		if(unuseful < 0) unuseful = 0;
		boolean isSuccess = comment.set("unuseful", ++unuseful).update();;
		if(isSuccess){	
			renderJson("result", true);
		}else{
			renderJson("result", false);
		}
	}
}
